# coding=utf-8
import scrapy
import huayiQDB.controller.huayiCollector as hcc
import huayiQDB.controller.huayiUtils as hcu
import huayiQDB.controller.log as log
import requests
import huayiQDB.controller.dbUtils as dbU

logger = log.logger


def Exam_navigation(response):
    """
    试题导航：根据pattern筛选：按国I类5分，国I类2、3分，市级继续教育项目，传染病项目，中医项目，人文项目
    :return:
    """
    logger.info("------------------------------Exam_navigation-------start----------------------------")
    if response.status == 200:
        NS_Info = response.meta['NS_Info']
        cookie = response.meta['cookie']
        db_sys = response.meta['db_sys']
        pattern = [
            '<div class="search_condition">(.*?)</div>',
            '<dl>(.*?)</dl>',
            '<dd>(.*?)</dd>',  # 筛选出导航<a>分类
            'onclick=\'setUrlParam2\("(.*?)"\)\'>'  # 进一步筛选单个导航分类明细
        ]
        #0、全员专项,1、国I类5分,2、国I类2、3分，3、市级继续教育项目，4、传染病项目，5、中医项目，5、人文项目
        num_type = 1
        class_list = hcc.nav_list(pattern, response.text)
        NS_Info['Class_Type'] = str(class_list[num_type]['tname'])
        # print(str(class_list[num_type]['tname']))
        url = 'http://cme2.91huayi.com/pages/course_list_v2.aspx?dept_id='+NS_Info['Item_Value']+'&tid='+class_list[num_type]['tid']+'&tname='+class_list[num_type]['tname']
        logger.info("------------------------------Exam_navigation-------endt----------------------------")
        return scrapy.Request(url, meta={'url': url, 'NS_Info': NS_Info, 'cookie': cookie,'db_sys':db_sys,'id':'parse_exam'},
                                 callback=parse_AllCourse, cookies=cookie, dont_filter=True)


def parse_onepage_course(response):
    """
    访问一页的课程
    :param response:
    :return:
    """
    # logger.info("------------------------------parse_onepage_course-------start----------------------------")
    # logger.info("response.status" + str(response.status))
    # logger.info("response.head" + str(response.headers))
    # logger.info("response.body" + str(response.text))
    cookie = response.meta['cookie']
    db_sys = response.meta['db_sys']
    if response.status == 200:
        pattern = [
            '<div class="item_r">(.*?)</dl>                        <br clear="all" />                    </div>                    <br clear="all" />',
            # 筛选所需信息分组
            '<img src="../images/(.*?)\.gif',  # 关注度
            'javascript:window.top.location.href=\'(.*?)\'" title=',  # 课程链接
            'title="(.*?)"><strong>',  # 课程名字
            '<div class="txts">(.*?)</div>',  # 拆分四个<div class="txts">分组，第一个学分，第二个项目编号，第三个项目负责人，第四个单位
            '<span class="f12blue">(.*?)</span>',  # 在第二个分组中抽取，第二个项目编号.  在第三个分组抽取，第三个项目负责人
            'title=\'(.*?)\'>'  # 在第四个分组中抽取 单位
        ]
        NS_Info = response.meta['NS_Info']
        # logger.info("url_referer:" + str(response.headers))
        course_list = hcc.collect_OnePageCourseInfo(pattern, response.text)
        for onecourse in course_list:
            Course_Info = {}
            url = "http://cme2.91huayi.com/pages/" + onecourse['item_url']
            Course_Value = hcu.getCID(onecourse['item_url'])
            Course_Name = onecourse['item_name']
            Course_score = onecourse['item_txts']
            Course_Info['Course_Value'] = Course_Value
            Course_Info['Course_Name'] = Course_Name
            Course_Info['Course_score'] = Course_score
            # logger.info('Course_Value' + str(Course_Value))
            # logger.info('Course_Name' + str(Course_Name))
            # logger.info("URL" + str(url))
            yield scrapy.Request(url, callback=parse_course,
                                 meta={'R_type': 'course', 'NS_Info': NS_Info, 'cookie': cookie,'Course_Info':Course_Info,'db_sys':db_sys,'course_value':Course_Value,'id':'course'}, cookies=cookie,
                                 dont_filter=True)
            # return scrapy.Request(url, callback=parse_course, meta={'request_type': request_type}) #测试单个学习
    # logger.info("------------------------------parse_onepage_course-------end----------------------------")

def parse_AllCourse(response):
    """
    处理首页课程并访问下一页，知道最后一页
    获取id="__VIEWSTATE"的value访问下一页
    :param response:
    :return:
    """
    # logger.info("------------------------------parse_AllCourse-------start----------------------------")
    # logger.info("response.status" + str(response.status))
    # logger.info("response.head" + str(response.headers))
    # logger.info("response.body" + str(response.text))
    cookie = response.meta['cookie']
    db_sys = response.meta['db_sys']
    course_nums = 0
    if response.status == 200:
        pattern_1 = r'<span id="labAll">(.*?)</span>'
        pattern_2 = r'<input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE" value="(.*?)"'
        pattern_3 = r'id="__VIEWSTATEGENERATOR" value="(.*?)"'
        try:
            course_nums = hcc.collectCourseInfo_Num(pattern_1, response.text)  # 课程数量
        except Exception as ex:
            logger.info("--------------collectCourseInfo_Num------start---------------------")
            logger.info(str(response.text))
            logger.info("异常：%s"%ex)
            logger.info("--------------collectCourseInfo_Num-------end--------------------")
        # logger.info("course_nums:"+str(course_nums))
        # logger.info(str(response.text))
        NS_Info = response.meta['NS_Info']
        if course_nums >0:
            VIEWSTATE = hcc.collectCoursePage_VIEWSTATE(pattern_2, response.text)
            VIEWSTATEGENERATOR = hcc.collectCoursePage_VIEWSTATEGENERATOR(pattern_3, response.text)

            # parse_onepage_course(response)  # 访问第一页

            # logger.info("------------------------------parse_onepage_course-------start----------------------------")
            # logger.info("response.status" + str(response.status))
            # logger.info("response.head" + str(response.headers))
            # logger.info("response.body" + str(response.text))
            pattern = [
                '<div class="item_r">(.*?)</dl>                        <br clear="all" />                    </div>                    <br clear="all" />',
                # 筛选所需信息分组
                '<img src="../images/(.*?)\.gif',  # 关注度
                'javascript:window.top.location.href=\'(.*?)\'',  # 课程链接
                'title="(.*?)"><strong>',  # 课程名字
                '<div class="txts">(.*?)</div>',  # 拆分四个<div class="txts">分组，第一个学分，第二个项目编号，第三个项目负责人，第四个单位
                '<span class="f12blue">(.*?)</span>',  # 在第二个分组中抽取，第二个项目编号.  在第三个分组抽取，第三个项目负责人
                'title=\'(.*?)\'>'  # 在第四个分组中抽取 单位
            ]
            # pattern = [
            #     '<span id="labAll">(.*?)</span>'
            # ]
            # logger.info("url_referer:" + str(response.headers))
            request_list = []  # 请求的列表，默认添加首页的课程
            course_list = []
            try:
                course_list = hcc.collect_OnePageCourseInfo(pattern, response.text)  # 收集一页中的课程
            except Exception as ex:
                logger.info("--------------collect_OnePageCourseInfo------start---------------------")
                logger.info(response.text)
                logger.info("异常：%s" % ex)
                logger.info("--------------collect_OnePageCourseInfo-------end--------------------")
            for onecourse in course_list:
                url = "http://cme2.91huayi.com/pages/" + onecourse['item_url']
                # logger.info("URL" + str(url))
                Course_Info={}
                Course_Value = hcu.getCID(onecourse['item_url'])
                Course_Name = onecourse['item_name']
                Course_score = onecourse['item_txts']
                Course_Info['Course_Value'] = Course_Value
                Course_Info['Course_Name'] = Course_Name
                Course_Info['Course_score'] = Course_score
                # logger.info('Course_Value' + str(NS_Info['Course_Value']))
                # logger.info('Course_Name' + str(NS_Info['Course_Name']))
                request_list.append(scrapy.Request(url, callback=parse_course,
                                                   meta={'R_type': 'course', 'NS_Info': NS_Info, 'cookie': cookie,'Course_Info':Course_Info,'db_sys':db_sys,'course_value':Course_Value,'id':'course'},
                                                   cookies=cookie, dont_filter=True))

            if course_nums > 8:
                real_num = int(course_nums / 8)  # real_num再次访问课程页的次数，依次第2、3、4....页
                if (course_nums % 8) > 0:
                    real_num = real_num + 1

                # 获取id="__VIEWSTATE"的value，带value访问其他页
                for i in range(real_num - 1):  # 访问剩余页,访问剩余页中的课程
                    url = str(response.meta['url'])
                    myFormData = {}
                    myFormData['__VIEWSTATE'] = VIEWSTATE
                    myFormData['__VIEWSTATEGENERATOR'] = VIEWSTATEGENERATOR
                    myFormData['__EVENTTARGET'] = 'AnpNewsClassList_1_1'
                    myFormData['__EVENTARGUMENT'] = str(i + 2)
                    # logger.info("URL:" + str(url))
                    # logger.info("myFormData" + str(myFormData))
                    # 访问下一页并处理下一页内容
                    request_list.append(scrapy.FormRequest(url, formdata=myFormData, callback=parse_onepage_course,
                                                           meta={'NS_Info': NS_Info, 'cookie': cookie,'db_sys':db_sys,'id':'next_page'}, cookies=cookie,
                                                           dont_filter=True))

            for oneRequest in request_list:
                yield oneRequest
        else:
            # logger.info("------------------------------parse_AllCourse-------end----------------------------")
            url = response.meta['url']
            yield scrapy.Request(url, callback=parse_exam, dont_filter=True,
                                               meta={'getRequest': 'getRequest','id':'item',
                                                     'NS_Info': NS_Info, 'cookie':cookie},
                                               cookies=cookie)

def parse_course(response):
    """
    访问单个的课程
    :param response:
    :return:
    """
    # logger.info("------------------------------parse_course-------start----------------------------")
    # logger.info("response.status" + str(response.status))
    logger.info("response.head" + str(response.headers))
    # logger.info("response.body" + str(response.text))

    cookie = response.meta['cookie']
    Course_Info = response.meta['Course_Info']
    db_sys = response.meta['db_sys']
    cookie = hcu.getCookie_fixNeW(cookie, response.headers)
    # logger.info("增加user_id后的cookie" + str(cookie))
    if response.status == 200:
        pattern = [
            '<div class="course">(.*?)</div>                            </div>',  # 各学习分组
            '<span>{1}<a href=\'\.\.(.*?)\' target=',  # url
            '<strong>(.*?)</strong>',  # 学习名称
            '<div class="txts"(.*?)</div>',  # 老师、单位分组
            '<span class="f12blue">(.*?)</span>',  # 授课老师、单位分别筛选
            '<img src=\'\.\.course_ware/images/(.*?)\'',
            'cwid=(.*?)\''
        ]
        NS_Info = response.meta['NS_Info']
        # logger.info('parse_course_NS_Info:------------------------')
        # logger.info('Item_Value:' + str(NS_Info['Item_Value']))
        # logger.info('Item_Name:' + str(NS_Info['Item_Name']))
        # logger.info('Course_Value:' + str(NS_Info['Course_Value']))
        # logger.info('Course_Name:' + str(NS_Info['Course_Name']))
        # logger.info("url_referer:" + str(response.headers))
        course_list = hcc.study_detail_AllList(pattern, response.text)
        parse_course = bool(True)
        for onestudy in course_list:
            # logger.info('------------onestudy-----before-----')
            Study_Info = {}
            Study_Info['Study_Name'] = str(onestudy['study_name'])
            Study_Info['Study_Value'] = str(onestudy['study_value'])
            # logger.info('Study_Name' + str(NS_Info['Study_Name']))
            # logger.info('Study_Value' + str(NS_Info['Study_Value']))
            url = "http://cme2.91huayi.com" + hcu.modifyUrl(onestudy['study_url'], "polyv")
            url_change = "http://cme2.91huayi.com" + hcu.modifyUrl(onestudy['study_url'], "cc")
            # logger.info('------------onestudy-----after-----')

            # logger.info('Study_Name' + str(NS_Info['Study_Name']))
            # logger.info('Study_Value' + str(NS_Info['Study_Value']))
            # logger.info("URL-----:" + str(url))
            # yield scrapy.Request(url, callback=parse_study, meta={'request_type': request_type, 'cookie': cookie, 'dont_redirect': True, 'handle_httpstatus_list': [302]}, cookies=cookie)
            yield scrapy.Request(url, callback=parse_study,
                                 meta={'parse_course': 'parse_course', 'NS_Info': NS_Info, 'cookie': cookie,
                                       'url': url_change,'Course_Info':Course_Info,'Study_Info':Study_Info,'db_sys':db_sys,'id':'parse_course'}, cookies=cookie, dont_filter=True)
            # return scrapy.Request(url, callback=parse_study,meta={'request_type': request_type, 'cookie': cookie,'url':url_change}, cookies=cookie)  #测试单个请求
    # logger.info("------------------------------parse_course-------end----------------------------")


def parse_study(response):
    """
    处理访问单个学习响应的页面信息
    :return:
    """
    logger.info("------------------------------parse_study-------start----------------------------")
    # logger.info("response.status" + str(response.status))
    # logger.info("response.head" + str(response.headers))
    # logger.info("response.body" + str(response.text))

    if response.status == 200:
        pattern_1 = r'var uid = \'(.*?)\';'
        pattern_2 = r'var cwrid = \'(.*?)\';'
        # pattern_3 = r'var cwid = \'(.*?)\';'
        pattern_3 = r'var cwid = \'(.*?)\';'
        cookie = response.meta['cookie']
        url_referer = response.meta['url']
        # logger.info("url_referer:" + str(response.headers))
        NS_Info = response.meta['NS_Info']
        Course_Info = response.meta['Course_Info']
        Study_Info = response.meta['Study_Info']
        db_sys = response.meta['db_sys']
        # logger.info('parse_study_NS_Info:------------------------')
        # logger.info('Item_Value:' + str(NS_Info['Item_Value']))
        # logger.info('Item_Name:' + str(NS_Info['Item_Name']))
        # logger.info('Course_Value:' + str(NS_Info['Course_Value']))
        # logger.info('Course_Name:' + str(NS_Info['Course_Name']))
        # logger.info('Study_Value:' + str(NS_Info['Study_Value']))
        # logger.info('Study_Name:' + str(NS_Info['Study_Name']))
        # 跳过视频 http://cme2.91huayi.com/ashx/add_course_ware_play_record.ashx?relation_id=60bd13ad-5b85-49b8-89d0-ab0200a04f19&user_id=ee68b7ab-fa51-4122-b4cb-ac4501035ec2
        # relation_id=60bd13ad-5b85-49b8-89d0-ab0200a04f19&user_id=ee68b7ab-fa51-4122-b4cb-ac4501035ec2
        # 考试连接http://cme2.91huayi.com/pages/exam.aspx?cwid=60bd13ad-5b85-49b8-89d0-ab0200a04f19

        relation_id = hcc.study_relation_id(pattern_1, response.text)
        # relation_id = ''
        # try:
        #     relation_id = hcc.study_relation_id(pattern_1, response.text)
        # except:
        #     check_nums = hcu.get_checknums(response.text)

        user_id = hcc.study_user_id(pattern_2, response.text)
        cwid = hcc.study_user_id(pattern_2, response.text)
        url_track = "http://cme2.91huayi.com/ashx/add_course_ware_play_record.ashx?relation_id=" + relation_id + "&user_id=" + user_id
        url_exam = "http://cme2.91huayi.com/pages/exam.aspx?cwid=" + cwid
        cookie['relation_id'] = relation_id
        # logger.info("跳视频的URL:" + str(url_track))

        header = {
            'X-Requested-With': 'XMLHttpRequest',
            'Referer': url_referer
        }
        header1 = {
            'Referer': url_referer
        }
        SkipVideo_response = requests.get(url_track, headers=header, cookies=cookie)
        # logger.info("SkipVideo_response的状态:" + str(SkipVideo_response))
        # logger.info("考试的URL:" + str(url_exam))
        order = 'parse_study'
        return scrapy.Request(url_exam, callback=parse_exam,
                              meta={'order': order, 'url_track': url_track, 'url_exam': url_exam, 'NS_Info': NS_Info,
                                    'cookie': cookie,'Course_Info':Course_Info,'Study_Info':Study_Info,'db_sys':db_sys,'id':'parse_study'}, cookies=cookie, headers=header, dont_filter=True)
    logger.info("------------------------------parse_study-------end----------------------------")


# def parse_study2(response):
#     """
#     从验证视频链接转回考试链接
#     :param response:
#     :return:
#     """
#     if response.status == 200:
#         logger.info("------------------------------parse_study2-------start----------------------------")
#         logger.info("response.status" + str(response.status))
#         logger.info("response.head" + str(response.headers))
#         logger.info("response.body" + str(response.text))
#         request_type = response.meta['request_type']
#         cookie = response.meta['cookie']
#         url = response.meta['url_exam']
#         return scrapy.Request(url,meta={'url_exam': url,'request_type': request_type, 'cookie':cookie}, callback=parse_exam, cookies=cookie)
#     logger.info("------------------------------parse_study2-------end----------------------------")


def parse_inspectExam(response):
    """
    通过提交结果，检查考试情况并做重考或保存正确答案处理
    :param response:
    :return:
    """
    logger.info("------------------------------parse_inspectExam-------start----------------------------")
    # logger.info("response.status" + str(response.status))
    # logger.info("response.head" + str(response.headers))
    # logger.info("response.body" + str(response.text))
    cookie = response.meta['cookie']
    NS_Info = response.meta['NS_Info']
    Course_Info = response.meta['Course_Info']
    Study_Info = response.meta['Study_Info']
    db_sys = response.meta['db_sys']
    # logger.info('parse_inspectExam_NS_Info:------------------------')
    # logger.info('Item_Value:' + str(NS_Info['Item_Value']))
    # logger.info('Item_Name:' + str(NS_Info['Item_Name']))
    # logger.info('Course_Value:' + str(NS_Info['Course_Value']))
    # logger.info('Course_Name:' + str(NS_Info['Course_Name']))
    # logger.info('Study_Value:' + str(NS_Info['Study_Value']))
    # logger.info('Study_Name:' + str(NS_Info['Study_Name']))
    already_all_correct = response.meta.get('already_all_correct')
    # logger.info("request_type:" + str(request_type))
    pattern_err = [
        '<h4>很抱歉，有(.*?)道题您答错了</h4>',
        '<dd title=\'(.*?)\'>',
        'value="重新学习" class="huise1" /><input name="" onclick="javascript: window\.location\.href = \'(.*?)\'',
        '<b style="float: left; margin-right: 0; margin-left: 20px;">(.*?)</b>'  # 获取 考试通过状态
    ]
    check_unrefresh_state = response.meta['check_unrefresh_state']
    # logger.info("check_unrefresh_state:" + str(check_unrefresh_state))
    if not check_unrefresh_state:
        check_unrefresh_state = 0
    url_exam = response.meta.get('url_exam')
    try:
        if url_exam:
            error = hcc.collect_ErrorExamResult1(pattern_err, response.text, url_exam)
        else:
            error = hcc.collect_ErrorExamResult2(pattern_err, response.text)
    except Exception:
        logger.info("response.body" + str(response.text))
    # logger.info("error_num:" + str(error['err_num']))
    quest_list = response.meta.get('quest_list')
    quest_list_history = response.meta.get('quest_list_history')
    # if quest_list:                              #打印
    #     for onequest in quest_list:
    #         logger.info("Q1:")
    #         logger.info("q_name:" + str(onequest['q_name']))
    #         logger.info("q_question_test:" + str(onequest['q_question_test']))
    #         logger.info("err_content_list:" + str(error['err_content_list']))
    #         logger.info("url_exam:" + str(error['url_exam']))

    if error['err_num']:  # 回答没有全对,更改错误选项再次测试
        for onequest in quest_list:
            isRightState = True
            for err_content in error['err_content_list']:
                Qname = str(onequest['q_name'])
                if err_content == Qname:
                    isRightState = False
                    if int(onequest['q_question_test']) == len(onequest['q_choiceValueList']) - 1:
                        logger.info(response.text)
                        logger.info("a_name_all:" + str(onequest['a_name_all']))
                        logger.info("a_name_all:" + str(onequest['a_name_all']))
                        logger.info("q_question_test:" + str(onequest['q_question_test']))
                        logger.info("q_question_correct:" + str(onequest['q_question_correct']))
                        logger.info("q_question_correct_name:" + str(onequest['q_question_correct_name']))
                        raise ValueError("测试异常,测试选项答案的次序大于实际已有的待选答案")
                    elif int(onequest['q_question_test']) == len(onequest['q_choiceValueList']) - 2:
                        onequest['q_question_correct'] = onequest['q_nameid'][int(onequest['q_question_test']) + 1]
                        onequest['q_question_correct_name'] = onequest['q_choiceValueList'][
                            int(onequest['q_question_test']) + 1]
                    elif int(onequest['q_question_test']) < len(onequest['q_choiceValueList']) - 2:
                        onequest['q_question_test'] = int(onequest['q_question_test']) + 1
                    else:
                        raise ValueError("所有测试答案没有结果")
                    continue
            if isRightState:
                onequest['q_question_correct'] = onequest['q_nameid'][int(onequest['q_question_test'])]
                onequest['q_question_correct_name'] = onequest['q_choiceValueList'][
                    int(onequest['q_question_test'])]
                # if int(onequest['q_question_test']) != 0:
                #     onequest['q_question_test'] = int(onequest['q_question_test']) - 1
                # else:
                #     onequest['q_question_test'] = int(onequest['q_question_test']) + 1

        # if quest_list:
        #     logger.info("-----------检查后的quest_list:----------------")
        #     for onequest in quest_list:  # 打印
        #         logger.info("q_name:" + str(onequest['q_name']))
        #         logger.info("url_exam:" + str(onequest['q_IDValue']))
        #         logger.info("q_question_test:" + str(onequest['q_question_test']))
        #         logger.info("q_question_correct:" + str(onequest['q_question_correct']))
        #         logger.info("q_question_correct_name:" + str(onequest['q_question_correct_name']))
        # logger.info("------------------------------parse_inspectExam-if-------end----------------------------")

        quest_list_history = hcu.updatequest_list(quest_list, quest_list_history)  # 根据上次考试结果更新quest_list_history

        # logger.info("再次请求答题时所带的quest_list:")
        # for onequest in quest_list:  #打印
        #     logger.info("q_name:" + str(onequest['q_name']))
        #     logger.info("q_question_test:" + str(onequest['q_question_test']))
        #     logger.info("err_content_list:" + str(error['err_content_list']))
        #     logger.info("url_exam:" + str(error['url_exam']))
        #     logger.info("q_question_correct:" + str(onequest['q_question_correct']))
        #     logger.info("q_question_correct_name:" + str(onequest['q_question_correct_name']))
        #
        # logger.info("再次请求答题时所带的quest_list_history:")
        # for onequest in quest_list_history:  # 打印
        #     logger.info("q_name:" + str(onequest['q_name']))
        #     logger.info("q_question_test:" + str(onequest['q_question_test']))
        #     logger.info("err_content_list:" + str(error['err_content_list']))
        #     logger.info("url_exam:" + str(error['url_exam']))
        #     logger.info("q_question_correct:" + str(onequest['q_question_correct']))
        #     logger.info("q_question_correct_name:" + str(onequest['q_question_correct_name']))

        url_exam = error['url_exam']
        if check_unrefresh_state > 5 and bool(already_all_correct):
            logger.info("全对后正确案:")
            # for onequest in quest_list_history:
            #     #onequest['q_question_correct'] = onequest['q_nameid'][int(onequest['q_question_test'])]
            #     logger.info("全对后此题目:" + str(onequest['q_name']))
            #     logger.info("全对后此题答案:" + str(onequest['q_question_correct']))
            #     logger.info("全对后此题答案描述:" + str(onequest['q_question_correct_name']))
            #     logger.info("全对后此题答案test:" + str(onequest['q_question_test']))
            #     logger.info("全对后此题答案集合:" + str(onequest['q_choiceValueList']))
            #     logger.info(
            #         "------------------------------parse_inspectExam-else-------end----------------------------")

            tem_list = dbU.changeItem(NS_Info, Course_Info, Study_Info,quest_list_history)  # 返回正确答案
            tem_list.append(scrapy.Request(url_exam, callback=parse_exam, dont_filter=True,
                                           meta={'getRequest': 'getRequest', 'anwsers': quest_list_history,
                                                 'study_value':Study_Info['Study_Value'],'id':'study',
                                                 'NS_Info': NS_Info,'Course_Info':Course_Info},
                                           cookies=cookie))
            return dbU.yieldAllItem(tem_list)
            # return scrapy.Request(url_exam, callback=parse_item, dont_filter=True, meta={'getRequest':'getRequest','anwsers':quest_list_history}, cookies=cookie)  # 访问重新考试
        return scrapy.Request(url_exam, callback=parse_exam, dont_filter=True,
                              meta={'url_exam': url_exam, 'quest_list_history': quest_list_history,
                                    'check_unrefresh_state': check_unrefresh_state, 'NS_Info': NS_Info,
                                    'cookie': cookie,'Course_Info':Course_Info,'Study_Info':Study_Info,
                                    'db_sys':db_sys,'id':'parse_inspectExam'}, cookies=cookie)  # 访问重新考试
    else:  # 全对，录入正确标记
        logger.info("------------------------------运气好全对后正确案:----start-----------------------")
        # for onequest in quest_list_history:
        #     onequest['q_question_correct'] = onequest['q_choiceValueList'][int(onequest['q_question_test'])]
        #     logger.info("全对后此题答案:" + str(onequest['q_question_correct']))
        #     logger.info("全对后此题答案:" + str(onequest['q_question_correct_name']))
        tem_list = dbU.changeItem(NS_Info, Course_Info, Study_Info,quest_list_history)  # 返回正确答案
        tem_list.append(scrapy.Request(url_exam, callback=parse_exam, dont_filter=True,
                                       meta={'getRequest': 'getRequest', 'anwsers': quest_list_history,
                                             'cookie': cookie,'study_value':Study_Info['Study_Value'],
                                             'id':'study','NS_Info': NS_Info,'Course_Info':Course_Info},
                                       cookies=cookie))
        # logger.info("------------------------------运气好全对后正确案:-------end----------------------------")
        return dbU.yieldAllItem(tem_list)


# def parse_item(response):
#     quest_list_history = response.meta.get('anwsers')
#     return dbU.changeItem(quest_list_history)

def parse_exam(response):
    # logger.info("------------------------------parse_exam-------start----------------------------")
    """
    收集考题并提交答案
    :param response:
    :return:
    提交考试http://cme2.91huayi.com/pages/exam_result.aspx?cwid=60bd13ad-5b85-49b8-89d0-ab0200a04f19
    pos  302
    get http://cme2.91huayi.com/pages/exam_result.aspx?cwid=60bd13ad-5b85-49b8-89d0-ab0200a04f19 获得结果

    http://cme2.91huayi.com/pages/exam_result.aspx?cwid=60bd13ad-5b85-49b8-89d0-ab0200a04f19
    参数：
    __EVENTTARGET 空
    __EVENTARGUMENT 空
    __VIEWSTATE  VIEWSTATE
    __VIEWSTATEGENERATOR  VIEWSTATEGENERATOR
    __EVENTVALIDATION  EVENTVALIDATION
    Hidden1  0
    Hidden2  0
    Hidden3  0
    gvQuestion$ctl02$rbl: 2d6adcbe-7d87-4833-bb7d-ab02009b78dd
    gvQuestion$ctl02$result: aiqdoqFSEv28kueSsii7o3vIm3AISj2T/iEQ9RzdYs7P7zdrd5qoJsixBHSu029q
    gvQuestion$ctl02$question_id: 572d264b-2d5c-472a-8848-ef43258bcdb6

    gvQuestion$ctl03$rbl: 63671dac-8d0e-4296-a8d6-ab02009b78dd
    gvQuestion$ctl03$result: eCxmgJ791PDidKsQJ15qZGWt6dRUPPm2S5oRbuFmJzvP7zdrd5qoJsixBHSu029q
    gvQuestion$ctl03$question_id: 9539aeb4-db71-4294-b1c8-ce44a71c1d59

    gvQuestion$ctl04$rbl: c8432a98-f0a5-4f1f-ae9c-ab02009b78dd
    gvQuestion$ctl04$result: JiN3G+Ht+HnINalMfci6EMPfGNmHNmQ3ULCqyOzohuTP7zdrd5qoJsixBHSu029q
    gvQuestion$ctl04$question_id: c0a40bbf-4aae-4713-9dec-7833e46e5f32

    gvQuestion$ctl05$rbl: d6d8a8d0-2a82-4d91-adc2-ab02009b78dd
    gvQuestion$ctl05$result: AwHliIXvj74KVcCLFqmY0V3QYzOBRV/kPgGzIlmW6RbP7zdrd5qoJsixBHSu029q
    gvQuestion$ctl05$question_id: 32f9a5ac-c096-4fd7-8801-f0648c436a31

    gvQuestion$ctl06$rbl: bd891f61-9ab0-49c0-9dd5-ab02009b78dd
    gvQuestion$ctl06$result: AdK/T7XEqD7jtng2fWYFslIDThPwanLj57sr5mM4X5PP7zdrd5qoJsixBHSu029q
    gvQuestion$ctl06$question_id: 5da80b40-3d2a-4994-bc74-0154828275d6
    btn_submit.x: 65
    btn_submit.y: 22
    """
    # logger.info("response.status" + str(response.status))
    # logger.info("response.head" + str(response.headers))
    # logger.info("response.body" + str(response.text))
    all_aready = False
    if response.status == 200:
        pattern_1 = [  # 获取访问参数标记
            'id="__VIEWSTATE" value="(.*?)"',
            'id="__VIEWSTATEGENERATOR" value="(.*?)"',
            'id="__EVENTVALIDATION" value="(.*?)"'
        ]
        pattern_exam = '<form method="post" action="\.(.*?)"'
        cookie = response.meta['cookie']
        NS_Info = response.meta['NS_Info']  # 获取考试题分类
        Course_Info = response.meta['Course_Info']
        Study_Info = response.meta['Study_Info']
        db_sys = response.meta['db_sys']
        # logger.info('parse_exam_NS_Info:------------------------')
        # logger.info('Item_Value:' + str(NS_Info['Item_Value']))
        # logger.info('Item_Name:' + str(NS_Info['Item_Name']))
        # logger.info('Course_Value:' + str(NS_Info['Course_Value']))
        # logger.info('Course_Name:' + str(NS_Info['Course_Name']))
        # logger.info('Study_Value:' + str(NS_Info['Study_Value']))
        # logger.info('Study_Name:' + str(NS_Info['Study_Name']))
        quest_list = hcc.question_detail_AllList(response, NS_Info['Item_Value'])

        # logger.info("quest_list......len..................:" + str(len(quest_list)))

        # hcu.fillRequestType(quest_list, request_type)
        quest_list_history = response.meta.get('quest_list_history')  # 获取上次考题及状态

        # logger.info("quest_list:")
        # for onequest in quest_list:  # 打印
        #     logger.info("q_name:" + str(onequest['q_name']))
        #     logger.info("url_exam:" + str(onequest['q_IDValue']))
        #     logger.info("q_question_test:" + str(onequest['q_question_test']))
        #     logger.info("q_question_correct:" + str(onequest['q_question_correct']))
        #

        check_unrefresh_state = 0
        if response.meta.get('check_unrefresh_state'):
            check_unrefresh_state = int(response.meta.get('check_unrefresh_state'))
        already_all_correct = False

        if quest_list_history:
            check_unrefresh_state = hcu.get_checkRefreshState(hcu.delNameOder(quest_list), quest_list_history,
                                                              check_unrefresh_state)  # 检测是否有新题出现，返回刷新多少次后没新题，不含连续情况;将新出现的题加入quest_list_history,刷新已知题在quest_list中的状态,并设置测试选项
            already_all_correct = hcu.isObtainAllCorrectAnswer(quest_list_history)
        else:
            quest_list_history = hcu.delNameOder(quest_list)
        # url='http://cme2.91huayi.com/pages/exam_result.aspx?cwid='+cwid  #组装URL
        # 结束条件四次以上没有出现新题,且得到全部答案

        myFormData = {}
        myFormData['__EVENTTARGET'] = ''
        myFormData['__EVENTARGUMENT'] = ''
        myFormData['__VIEWSTATE'] = hcu.selectInfo(pattern_1[0], response.text)[0]
        myFormData['__VIEWSTATEGENERATOR'] = hcu.selectInfo(pattern_1[1], response.text)[0]
        myFormData['__EVENTVALIDATION'] = hcu.selectInfo(pattern_1[2], response.text)[0]
        myFormData['Hidden1'] = '0'
        myFormData['Hidden2'] = '0'
        myFormData['Hidden3'] = '0'
        myFormData['btn_submit.x'] = str(hcu.randomInrange(40, 70))
        myFormData['btn_submit.y'] = str(hcu.randomInrange(20, 30))
        for onequest in quest_list:
            myFormData[onequest['q_choiceNameList'][0]] = onequest['q_nameid'][int(onequest['q_question_test'])]
            myFormData[onequest['q_resultName']] = onequest['result_value']
            myFormData[onequest['q_IDName']] = onequest['q_IDValue']

        # logger.info("考题状态：" + str(myFormData))
        # logger.info("URL:" + str(response.meta['url_exam']))

        url = str(response.meta['url_exam'])

        # http://cme3.91huayi.com/pages/exam.aspx?cwid=xxxxxxxxxxxx
        # http://cme3.91huayi.com/pages/exam_result.aspx?cwid=xxxxxxxxxxx

        url_exam = 'http://cme2.91huayi.com/pages' + str(hcu.selectInfo(pattern_exam, response.text)[0])
        url_result_exam = 'http://cme2.91huayi.com/pages' + hcu.modifyUrl(
            str(hcu.selectInfo(pattern_exam, response.text)[0]), 'result')

        # logger.info("交卷的信息：")
        # logger.info("url_result_exam："+url_result_exam)

        # for key,value in myFormData.items():
        #     logger.info(key+":" + str(value))

        header = {
            'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9',
            'Accept-Encoding': 'gzip, deflate',
            'Content-Type': 'application/x-www-form-urlencoded',
            'Connection': 'keep-alive',
            'Accept-Language': 'zh-CN,zh;q=0.9',
            'Cache-Control': 'max-age=0'
        }

        # if quest_list:
        #     logger.info("-----------quest_list:----------------")
        #     for onequest in quest_list:  # 打印
        #         logger.info("q_name:" + str(onequest['q_name']))
        #         logger.info("url_exam:" + str(onequest['q_IDValue']))
        #         logger.info("q_question_test:" + str(onequest['q_question_test']))
        #         logger.info("q_question_correct:" + str(onequest['q_question_correct']))
        #         logger.info("q_question_correct_name:" + str(onequest['q_question_correct_name']))
        #
        # if quest_list_history:
        #     logger.info("-----------quest_list_history2:----------------")
        #     for onequest in quest_list_history:  # 打印
        #         logger.info("q_name:" + str(onequest['q_name']))
        #         logger.info("url_exam:" + str(onequest['q_IDValue']))
        #         logger.info("q_question_test:" + str(onequest['q_question_test']))
        #         logger.info("q_question_correct:" + str(onequest['q_question_correct']))
        #         logger.info("q_question_correct_name:" + str(onequest['q_question_correct_name']))
        return scrapy.FormRequest(url_exam, formdata=myFormData, headers=header, callback=parse_inspectExam,
                                  meta={'url_exam': url_exam, 'url_result_exam': url_result_exam,
                                        'quest_list': quest_list, 'quest_list_history': quest_list_history,
                                        'check_unrefresh_state': check_unrefresh_state, 'NS_Info': NS_Info,
                                        'cookie': cookie, 'already_all_correct': already_all_correct,'Course_Info':Course_Info,'Study_Info':Study_Info,'db_sys':db_sys,'id':'parse_exam'}, cookies=cookie,
                                  dont_filter=True)  # 访问下一页并处理下一页内容
    # logger.info("------------------------------parse_exam-------end----------------------------")
